<template>
  <div class="mainos">
    <div class="main_spac1" style="padding-bottom: 5px">
      <div class="main_white padding10 paddingBottom20">
        <div class="main_crumbs boxdq">
          <div class="main_crumbs_l">
            <a href="javascript:;" @click="$router.back(-1)">
              <i class="el-icon-arrow-down"></i>返回
            </a>
            <em>|</em>
            <span>查看退货单</span>
          </div>
          <div>
            <el-button v-repeatClick @click="exportOrderiInfo">导出</el-button>
          </div>
        </div>
        <div class="main_tile boxdq marginTop10">
          <div class="main_tile_l marginLeft0"><em></em>单据信息</div>
        </div>
        <div class="main_xq">
          <ul class="boxha">
            <li>
              退货单号：<span>{{ info.order_sn }}</span>
            </li>
            <li>
              采购方式：<span>{{ base.modeList[info.mode] }}</span>
            </li>
            <li>
              采购员/供应商：<span>{{
                info.mode == 1
                  ? base.buyer[info.buyer_id]
                  : base.supplier[info.supplier_id]
              }}</span>
            </li>
            <li>
              退货金额：<span>{{ info.money }}</span>
            </li>
            <li>
              单据状态：<span>{{ base.status[info.status] }}</span>
            </li>
            <li>
              创建时间：<span>{{
                parseTime(info.created_at, "{y}-{m}-{d}")
              }}</span>
            </li>
          </ul>
        </div>
        <div class="main_tile boxdq marginTop10">
          <div class="main_tile_l marginLeft0"><em></em>商品详情</div>
          <div class="mainos_tile_r">合计：{{ total }}元</div>
        </div>
        <div class="main_table marginTop0">
          <el-table
            ref="multipleTable"
            v-loading="listLoading"
            :data="info.purchase_return_list"
            element-loading-text="加载中"
            fit
            highlight-current-row
          >
            <el-table-column label="序号" prop="userId">
              <template slot-scope="scope">{{ scope.$index + 1 }}</template>
            </el-table-column>
            <el-table-column
              label="商品名称"
              width="150"
              prop="name"
              class-name="main_hei"
            >
              <template slot-scope="scope"
                >{{ scope.row.goods.name }}【{{
                  scope.row.goods_spu.spu_name.title
                }}】</template
              >
            </el-table-column>
            <el-table-column
              label="单位"
              prop="goods_spu.spu_unit.title"
              class-name="main_hei"
            />
            <el-table-column
              label="退货数量"
              width="150"
              prop="num"
              class-name="main_hei"
            />
            <el-table-column
              label="退货单价"
              width="150"
              prop="price"
              class-name="main_hei"
            />
            <el-table-column
              label="退货金额"
              width="150"
              prop="money"
              class-name="main_hei"
            >
              <template slot-scope="scope">{{
                (
                  parseFloat(scope.row.num) * parseFloat(scope.row.price)
                ).toFixed(2)
              }}</template>
            </el-table-column>
            <el-table-column label="备注" prop="remark" class-name="main_hei" />
          </el-table>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {
  purchaseReturn_base,
  purchaseReturn_info,
  export_purchasing_return,
} from "@/api/purchasing/purchasingReturn";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";
import { parseTime } from "@/utils/index";

export default {
  computed: {
    total() {
      let sz = 0;
      this.info.purchase_return_list.forEach((e) => {
        sz += parseFloat(e.num * e.price);
      });
      return sz.toFixed(2);
    },
  },
  components: {},
  data() {
    return {
      // 时间
      parseTime,
      // 基本信息
      info: {
        created_at: "2020-10-20",
        type: 0,
        status: 0,
        mode: 0,
        purchase_return_list: [],
      },
      base: {
        modeList: ["后台"],
        status: ["后台"],
        buyer: ["后台"],
        supplier: ["后台"],
        typeList: ["待审核"],
      },
      listLoading: false,
      // id
      id: this.$route.params.id,
    };
  },
  created() {
    // 基本信息
    this.purchaseReturn_info();
    this.purchaseReturn_base();
  },
  methods: {
    // 基本信息
    purchaseReturn_info() {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, { id: this.id })) +
              apiUrl().appsecret
          ),
        },
        apiUrl().name,
        { id: this.id }
      );
      purchaseReturn_info(data).then((res) => {
        if (res.errcode == 0) {
          this.info = res.data.info;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    purchaseReturn_base() {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
          ),
        },
        apiUrl().name
      );
      purchaseReturn_base(data).then((res) => {
        if (res.errcode == 0) {
          this.base = res.data;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    // 订单导出
    exportOrderiInfo() {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        { id: this.id }
      );
      export_purchasing_return(data)
        .then((res) => {
          const link = document.createElement("a");
          let blob = new Blob([res], { type: "application/vnd.ms-excel" });
          link.style.display = "none";
          link.href = URL.createObjectURL(blob);
          document.body.appendChild(link);
          link.click();
          document.body.removeChild(link);
        })
        .catch(() => {
          this.$message.error("导出失败");
        });
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/styles/variables.scss";

.mainos {
  .main_tile {
    .mainos_tile_r {
      color: $colour_red;
      font-size: $font_14;
    }
  }
}
</style>
